<?php
namespace app\Capital\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Model;
use think\Db;
use app\Capital\model\capital as CapitalModel;
use think\Session;
use app\Capital\model\account;
class Shopcapital extends Admin{
    public function index(){
        $search=input('search');
        $where=[];
        if($search){
            $where['shopid|shop_name']=['like',"%$search%"];
            $this->assign('search',$search);
        }
        $startDate=input('date1');
        $endDate=input('date2');
        if($startDate && $endDate){
            $this->assign('startDate',$startDate);
            $this->assign('endDate',$endDate);
            $startDate = strtotime($startDate . ' 0:00:00');
            $endDate = strtotime($endDate . " 23:59:59");
            //    $where['createtime']=['between',[$startDate,$endDate]];
        }
        if($where){
            $data=Db::name('shop_account')->where($where)->order('createtime')->select();
        }else{
            $data=Db::name('shop_account')->order('createtime')->select();
        }
        $member=[];
        foreach($data as $value){
            if(isset($member[$value['shopid']])){
                if(isset($member[$value['shopid']][$value['account_type']])){
                    array_push($member[$value['shopid']][$value['account_type']],$value);
                }else{
                    $member[$value['shopid']][$value['account_type']][]=$value;
                }

            }else{
                $member[$value['shopid']][$value['account_type']][]=$value;
            }
        }
        $account=[];
        foreach($member as $k=>$item){
            foreach($item as $kk=> $vv){
                $account[$k][$kk]=$vv;
                foreach($vv as $v){
                    if(isset($account[$k][$kk]['type'][$v['type']])){
                        $account[$k][$kk]['type'][$v['type']]['num']+=$v['num'];
                        $account[$k][$kk]['type']['closing_balance']=$v['balance']+$v['num'];
                    }else{
                        $account[$k][$kk]['type'][$v['type']]['num']=$v['num'];
                        $account[$k][$kk]['type']['closing_balance']=$v['balance']+$v['num'];
                    }
                }
            }
        }
        //  dump($account);exit();
        $this->assign('account',$account);
        return $this->fetch('shop');
    }

    //店铺列表
    public function shoplist(){
        $search=input('q');
        $where['Name']=['like',"%$search%"];
        $shop=Db::name('shop')->field('ID,Name')->where($where)->select();
        $shoplist=[];
        $a= 0;
        foreach($shop as $value){
            $b = $a++;
            $key=$value['ID'].'-'.$value['Name'];
            $shoplist[$b]['ID']=$key;
            $shoplist[$b]['Name']=$value['Name'];
        }
        return json(['items'=>$shoplist,'total_count'=>sizeof($shoplist)]);
    }



    //单据操作
    public function alledit(){
        dump(input());
    }


    //店铺余额信息详情
    public function shop_account(){
        $shopid=input('shopid');
        $startDate=input('startDate');
        $endDate=input('endDate');
        $where=[];
        if($startDate && $endDate){
            $this->assign('startDate',$startDate);
            $this->assign('endDate',$endDate);
            $startDate = strtotime($startDate . ' 0:00:00');
            $endDate = strtotime($endDate . " 23:59:59");
            //$where['createtime']=['between',[$startDate,$endDate]];
        }
        if($shopid){
            $where['shopid']=$shopid;
        }
        if($where){
            $data=Db::name('shop_account')->where($where)->select();
        }
        $order_type=account::$orderType;
        $this->assign('account',$data);
        //  dump($data);exit();
        $this->assign('order_type',$order_type);
        $this->assign('type',[1=>'普通账户',2=>'信用额度']);
        return $this->fetch('shop_account');
    }

}